package cn.school.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.school.mybatis.pojo.Admin;

/**
 * Servlet implementation class MainServlet
 */
//通配符的请求地址
@WebServlet("*.do")
public class MainServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public MainServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.getWriter().print("login action");
//		request.setCharacterEncoding("utf-8");
//		String u = request.getParameter("userName");
//		String p = request.getParameter("userPass");
//		Admin admin = adminService.login(u, p);
//		if (admin != null) {
//			HttpSession s = request.getSession();
//			s.setAttribute("USER_LOGIN", admin);
//
//			// 转发
//			request.getRequestDispatcher("/pages/welcome.jsp").forward(request, response);
//			return;
//		}
//
//		response.sendRedirect("pages/fail.jsp");

	}

	protected void register(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.getWriter().print("register action");
	}

	protected void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.getWriter().print("update action");
	}

	protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().print("delete action");
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String path = request.getServletPath();
		String action = path.substring(1, path.length() - 3);
		try {
			if (action.equals("doGet") || action.equals("doPost") || action.equals("service")) {
				response.sendError(500, "非法请求");
				return;
			}
			Method method = this.getClass().getDeclaredMethod(action, HttpServletRequest.class,
					HttpServletResponse.class);
			method.setAccessible(true);
			method.invoke(this, request, response);
		} catch (Exception e) {
			response.getWriter().print("error=>" + e.getMessage());
			e.printStackTrace();
		}

		// if ("login".equals(action)) {
//			login(request, response);
//		} else if ("register".equals(action)) {
//			register(request, response);
//		} else if ("update".equals(action)) {
//			update(request, response);
//		} else {
//			response.getWriter().println("未知操作");
//		}

//		response.setContentType("text/html;charset=utf-8");
//		PrintWriter out = response.getWriter();
//		out.print("<html>");
//		out.print("<body>");
//		out.print("<h1>");
//		out.print("contextpath=" + request.getContextPath());
//		out.print("</h1>");
//		out.print("<h1>");
//		out.print("URI=" + request.getRequestURI());
//		out.print("</h1>");
//		out.print("<h1>");
//		out.print("URL=" + request.getRequestURL());
//		out.print("</h1>");
//		out.print("<h1>");
//		out.print("QueryString=" + request.getQueryString());
//		out.print("</h1>");
//		out.print("<h1 style='color:red'>");
//		out.print("servletpath=" + request.getServletPath());
//		out.print("</h1>");
//
//		out.print("<h1 style='color:red'>");
//		if ("login".equals(action)) {
//			out.println("执行登陆操作");
//			String u = request.getParameter("userName");
//
//		} else if ("register".equals(action)) {
//			out.println("执行注册操作");
//		} else if ("update".equals(action)) {
//			out.println("执行修改操作");
//		} else {
//			out.println("未知操作");
//		}
//		out.print("</h1>");
//
//		out.print("</body>");
//		out.print("</html>");
//		out.flush();
//		String action = request.getParameter("action");
//		if ("login".equals(action)) {
//			System.out.println("执行登陆操作");
//		} else if ("register".equals(action)) {
//			System.out.println("执行注册操作");
//		} else if ("update".equals(action)) {
//			System.out.println("执行修改操作");
//		} else {
//			System.out.println("未知操作");
//		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
